<?php

/**
 * Project actions.
 *
 * @package    ProdAssistant
 * @subpackage Project
 * @author     Paweł Świtalla
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class ProjectActions extends sfActions
{
  private $core;
  private $url;
  
  public function preExecute()
  {
    $this->core = PACore::getInstance();
    $this->url = $this->core->generateAppUrl('Core', 'productionSettings');
  }
  public function executeCreate(sfWebRequest $request)
  {
    $this->form = new ProjectForm();
    if($request->isMethod('post'))
    {
      $this->form->bind($request->getParameter('project'));
      if($this->form->isValid())
      {
        $project = $this->form->save();
        if($request->isXmlHttpRequest())
        {
          $this->getResponse()->setContent('location@'.$request->getReferer()."#");
          return sfView::NONE;
        }
        else $this->redirectIf($project, $this->url);;
      }
    }
    if($request->isXmlHttpRequest())
    {
      $errors = '';
      foreach($this->form->getFormFieldSchema() as $field){
        if($field->hasError()){
          $errors .= ucfirst($field->getName()).': '.$field->getError()."\n";
        }
      }
      $this->getResponse()->setContent('error@'.$errors."#");
      return sfView::NONE;
    }
    else $this->referer = $this->url;
  }
  
  public function executeDelete(sfWebRequest $request)
  {
    $project = Doctrine_Core::getTable('Project')->find($request->getParameter('id'));
    if($project instanceof Project)
    {
      $project->delete();
      $this->redirect($request->getReferer());
    }
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $project = Doctrine_Core::getTable('Project')->find($request->getParameter('id'));
    $this->form = new ProjectForm($project);
    if($request->isMethod('put'))
    {
      $this->form->bind($request->getParameter('project'));
      if($this->form->isValid())
      {
        $project = $this->form->save();
        if($request->isXmlHttpRequest())
        {
          $this->getResponse()->setContent('location@'.$this->url."#");
          return sfView::NONE;
        }
        else $this->redirectIf($project, $this->url);;
      }
    }
    if($request->isXmlHttpRequest())
    {
      $errors = '';
      foreach($this->form->getFormFieldSchema() as $field){
        if($field->hasError()){
          $errors .= ucfirst($field->getName()).': '.$field->getError()."\n";
        }
      }
      $this->getResponse()->setContent('error@'.$errors."#");
      return sfView::NONE;
    }
    else $this->referer = $this->url;
  }
  
  public function executeShow(sfWebRequest $request)
  {
  
  }
  
  public function executeProjectsList(sfWebRequest $request)
  {
    
  }
}
